package com.zyj.borrow.controller;

import com.zyj.common.annotation.Log;
import com.zyj.common.core.controller.BaseController;
import com.zyj.common.core.domain.AjaxResult;
import com.zyj.common.core.page.TableDataInfo;
import com.zyj.common.enums.BusinessType;
import com.zyj.common.utils.poi.ExcelUtil;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.service.IAssetsReceiveService;
import com.zyj.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产领用Controller
 * 
 * @author zhuyujie
 * @date 2022-05-09
 */
@Controller
@RequestMapping("/assets/borrow")
public class AssetsBorrowController extends BaseController
{
    private String prefix = "assets/borrow";

    @Autowired
    private IAssetsReceiveService assetsReceiveService;

    @Autowired
    private ISysUserService iSysUserServiceSysUserService;


    @GetMapping()
    public String receive()
    {
        return prefix + "/borrow";
    }

    @GetMapping("/timeLimitlist")
    public String timeLimitlist()
    {
        return prefix + "/timeLimitlist";
    }

    /**
     * 查询资产领用列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetsReceive assetsReceive)
    {
        startPage();
        List<AssetsReceive> list = assetsReceiveService.selectAssetsReceiveList(assetsReceive);
        return getDataTable(list);
    }

    //逾期归还列表
    @PostMapping("/predictedlist")
    @ResponseBody
    public TableDataInfo predictedlist(AssetsReceive assetsReceive)
    {
        startPage();
        List<AssetsReceive> list = assetsReceiveService.selectAssetsReceivePredictedList(assetsReceive);
        return getDataTable(list);
    }
    /**
     * 导出资产领用列表
     */
    @Log(title = "资产领用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetsReceive assetsReceive)
    {
        List<AssetsReceive> list = assetsReceiveService.selectAssetsReceiveList(assetsReceive);
        ExcelUtil<AssetsReceive> util = new ExcelUtil<AssetsReceive>(AssetsReceive.class);
        return util.exportExcel(list, "资产领用数据");
    }

    /**
     * 新增资产领用
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 从员工资产新增资产领用
     */
    @GetMapping("/add/{userid}")
    public String add(@PathVariable("userid") Long userid, ModelMap mmap)
    {
        mmap.put("userid", userid);
        mmap.put("username", iSysUserServiceSysUserService.selectUserById(userid).getUserName());
        return prefix + "/add";
    }

    /**
     * 新增保存资产领用
     */
    @Log(title = "资产领用", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetsReceive assetsReceive)
    {
        assetsReceive.setCreateBy(getLoginName());
        return toAjax(assetsReceiveService.insertAssetsReceive(assetsReceive));
    }

    /**
     * 修改资产领用
     */
    @GetMapping("/edit/{receiveId}")
    public String edit(@PathVariable("receiveId") Long receiveId, ModelMap mmap)
    {
        AssetsReceive assetsReceive = assetsReceiveService.selectAssetsReceiveByReceiveId(receiveId);
        mmap.put("assetsReceive", assetsReceive);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产领用
     */
    @Log(title = "资产领用", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetsReceive assetsReceive)
    {
        return toAjax(assetsReceiveService.updateAssetsReceive(assetsReceive));
    }

//    /**
//     * 删除资产领用
//     */
//    @RequiresPermissions("assets:receive:remove")
//    @Log(title = "资产领用", businessType = BusinessType.DELETE)
//    @PostMapping( "/remove")
//    @ResponseBody
//    public AjaxResult remove(String ids)
//    {
//        return toAjax(assetsReceiveService.deleteAssetsReceiveByReceiveIds(ids));
//    }


    /**
     * 选择用户
     */
    @GetMapping("/selectUser")
    public String selectUser(ModelMap mmap)
    {
        return prefix + "/selectUser";
    }

    /**
     * 选择设备
     */
    @GetMapping("/selectAssets/{status}")
    public String selectAssets(@PathVariable("status") String status,ModelMap mmap)
    {
        System.out.println("status="+status);
        mmap.put("status", status);
        return prefix + "/selectAssets";
    }

    //TODO 逾期列表
}
